package concurrency.tij;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

class TaskwithResult implements Callable<String>{
	private int id;
	
	public TaskwithResult(int id) {
		this.id = id;
	}

	public String call() throws Exception {
		return "result of TaskwithResult: " + id;
	}
}

public class CallableDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ExecutorService exe = Executors.newCachedThreadPool();
		List<Future<String>> result = new ArrayList<Future<String>>();
		
		for(int i = 0; i < 10; i++)
			result.add(exe.submit(new TaskwithResult(i)));
		
		for(Future<String> fs : result){
			try {
				System.out.println(fs.get());
			} catch (InterruptedException e) {
				e.printStackTrace();
				return;
			} catch (ExecutionException e) {
				e.printStackTrace();
			}finally{
				exe.shutdown();
			}
		}
	}
}
